package com.servlet;

import java.awt.Frame;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import com.xiaonei.DAO.PostDAOImpl;
import com.xiaonei.DAO.TopicDAOImpl;
import com.xiaonei.DAO.User1DAO;

/**
 * Servlet implementation class PostServlet
 */
// @WebServlet("/PostServlet")
public class NewPostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewPostServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean exc = false;

		request.setCharacterEncoding("utf-8");
		System.out.println("鐐瑰嚮");
		String SschoolNum = request.getParameter("schoolNum");
		int schoolNum = Integer.parseInt(SschoolNum);
		String content = request.getParameter("content");
		String theme = request.getParameter("theme");
		String topicContent = request.getParameter("topic");

		
		String account = (String) request.getSession().getAttribute("user");
		User1DAO user1DAO = new User1DAO();

		int topicNum = 0;
		String imageUrl = null;

		Date datetime = new Date();// 鑾峰彇绯荤粺鏃堕棿
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 璁剧疆鏃ユ湡鏍煎紡
		String time = df.format(datetime);
		

		// 鐢╰opicContent寰楀埌topicNum锟�
		TopicDAOImpl topicDAOImpl = new TopicDAOImpl();
		topicNum = topicDAOImpl.getIdOfTopic(topicContent);
		System.out.println("璇濋ID锟" + topicNum);
		
		System.out.println("查询到的学校编号是"+user1DAO.getSchoolNumByAccount(account));
		if (schoolNum == user1DAO.getSchoolNumByAccount(account)) {
			PostDAOImpl postDAOImpl = new PostDAOImpl();
			exc = postDAOImpl.insertIntoPost(content, theme, account, topicNum,
					time, imageUrl);
			if (!exc) {
				System.out.println("插入失败，哈�");
				// request.getRequestDispatcher("/tiezi/yanzhongcuowu.jsp").forward(request,
				// response);
			} else {
				request.getRequestDispatcher(
						"/which-uni.jsp?num=" + schoolNum + "&iPage=1")
						.forward(request, response);
			}
		} else {
			System.out.println("account不是在这个学校的。");
			JOptionPane.showMessageDialog(new Frame(),
					"sorry,您不是该学校的学生，不能在此发帖!");
			request.getRequestDispatcher(
					"/which-uni.jsp?num=" + schoolNum + "&iPage=1").forward(
			request, response);
		}
	}
}